package com.ws.shop.service.impl;

import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.ws.shop.dao.AsynGetTreeMenuDao;
import com.ws.shop.entity.AdminMenu;
import com.ws.shop.service.AsynGetTreeMenuService;

@Service(value="asynGetTreeMenuService")
public class AsynGetTreeMenuServiceImpl implements AsynGetTreeMenuService {
	@Resource(name="asynGetTreeMenuDao")
	private AsynGetTreeMenuDao asynGetTreeMenuDao;

	public void createxml(Map<Integer, AdminMenu> menumap, PrintWriter out,
			int id) {
		StringBuilder sb=new StringBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		sb.append("<tree id=\""+id+"\">");
		AdminMenu b=null;
		if(id==0){
			Set<Integer> set=menumap.keySet();
			for(Iterator<Integer> it=set.iterator();it.hasNext();){
				b=menumap.get(it.next());
				if(b!=null&&b.getPmenuid()==0){
					sb.append("<item text=\""+b.getMenuname()+"\" id=\""+b.getMenuid()+"\" open=\"true\">");
					List<AdminMenu> list=b.getChildren();
					for(int i=0;i<list.size();i++){
						sb.append(creatitemutil(list.get(i)));
					}
					sb.append("</item>");
				}
			}
		}else{
			b=menumap.get(id);
			List<AdminMenu> list=b.getChildren();
			for(int i=0;i<list.size();i++){
				sb.append(creatitemutil(list.get(i)));
			}
		}
		sb.append("</tree>");
		out.print(sb);
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public  Map<Integer, AdminMenu> getalltreemenu(){
		return asynGetTreeMenuDao.getalltreemenu();
	}
	
	@SuppressWarnings("unchecked")
	public void createxml_qx(Map<Integer, AdminMenu> menumap, PrintWriter out, int i) throws Exception {
		Map<Integer, AdminMenu> allmenumap=getalltreemenu();
		DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
		DocumentBuilder db=dbf.newDocumentBuilder();
		Document dom=db.newDocument();
		Element e=dom.createElement("tree");
		e.setAttribute("id", "0");
		e.setIdAttribute("id", true);
		dom.appendChild(e);
		AdminMenu b=null;
		Element e1=null;
		Element e2=null;
		while(allmenumap.size()>0){
			Set<Integer> set=allmenumap.keySet();
			for(Iterator<Integer> it=set.iterator();it.hasNext();){
				b=allmenumap.get(it.next());
				e1=dom.getElementById(b.getPmenuid()+"");
				if(e1!=null){
					e2=dom.createElement("item");
					e2.setAttribute("id", b.getMenuid()+"");
					e2.setAttribute("text", b.getMenuname());
					e2.setIdAttribute("id", true);
					//e2.setAttribute("checked", "0");
					e2.setAttribute("open", "1");
					e1.appendChild(e2);
					it.remove();
				}
			}
		}
		Set menuSet=menumap.keySet();
		Element e3=null;
		for(Iterator<Long> it=menuSet.iterator();it.hasNext();){
			b=menumap.get(it.next());
			e3=dom.getElementById(b.getMenuid()+"");
			if(e3!=null&&!e3.hasChildNodes()){
				e3.setAttribute("checked", "1");
			}	
		}
		TransformerFactory tranFactory = TransformerFactory.newInstance();
	    Transformer aTransformer = tranFactory.newTransformer();
	    aTransformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); 
	    aTransformer.setOutputProperty(OutputKeys.INDENT, "yes"); 
	    Source src = new DOMSource(dom);
	    Result dest = new StreamResult(out);
	    aTransformer.transform(src, dest);
	}

	private String creatitemutil(AdminMenu b){
		StringBuilder sb=new StringBuilder();
		if(b.getChildren().size()==0){
			sb.append("<item text=\""+b.getMenuname()+"\" id=\""+b.getMenuid()+"\"/>");
		}else{
			sb.append("<item text=\""+b.getMenuname()+"\" id=\""+b.getMenuid()+"\" child=\"1\"/>");
		}
		return sb.toString();
	}

	public Map<Integer, AdminMenu> getTreemenuByRoleid(int roleid) {
		return asynGetTreeMenuDao.getTreemenuByRoleid(roleid);
	}

}
